package com.yuke.cloud.service.omc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoExDto;
import com.yuke.cloud.service.omc.entity.OdsTeam;
import com.yuke.cloud.service.omc.entity.OdsTeamSpec;
import com.yuke.cloud.service.omc.service.OdsTeamService;
import com.yuke.cloud.service.omc.service.OdsTeamSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* Created by zn on 2020/10/05.
*/
@Api(value = "WEB - OdsTeamSpecController", tags = "处理OdsTeamSpec相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/teamSpec")
public class OdsTeamSpecController extends BaseController {
    @Resource
    private OdsTeamSpecService odsTeamSpecService;
    @Resource
    private OdsTeamService odsTeamService;


    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsTeamSpec", value = "该数据记录的POJO") @RequestBody OdsTeamSpec odsTeamSpec) {
        odsTeamSpec.setTeamSpecId(generateId());
        odsTeamSpecService.save(odsTeamSpec);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsTeamSpecService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsTeamSpec", value = "需要修改的内容") @RequestBody OdsTeamSpec odsTeamSpec) {
        odsTeamSpecService.update(odsTeamSpec);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsTeamSpec> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsTeamSpec odsTeamSpec = odsTeamSpecService.findById(id);
        return WrapMapper.ok(odsTeamSpec);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsTeamSpec>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsTeamSpec> list = odsTeamSpecService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/getTeamSpecList")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsTeamSpec>> getTeamSpecList(@ApiParam(name = "teamId", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Long teamId,
                                                @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);

        Example example = new Example(OdsTeamSpec.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort asc");
        criteria.andEqualTo("teamId",teamId);
        List<OdsTeamSpec> list = odsTeamSpecService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 查找拼单商品信息
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/selectTeamSpecList")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdSpecInfoExDto>> selectTeamSpecList(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                          @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        Date dateTime = new Date();
        Example exam = new Example(OdsTeamSpec.class);
        Example.Criteria crit = exam.createCriteria();
        exam.setOrderByClause("team_id desc");
        crit.andEqualTo("teamType",1);
        crit.andLessThan("bginTime",dateTime);
        crit.andGreaterThanOrEqualTo("endTime",dateTime);
        List<OdsTeam> teamList = odsTeamService.selectByExample(exam);
        if(teamList == null || teamList.size() < 1){
           return WrapMapper.error("未找到开团信息！");
        }
        Long teamId = teamList.get(0).getTeamId();

        PageHelper.startPage(page, size);
        Example example = new Example(OdsTeamSpec.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort asc");
        criteria.andEqualTo("teamId",teamId);
        List<OdsTeamSpec> list = odsTeamSpecService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);

        List<ProdSpecInfoExDto> storageIds = odsTeamSpecService.getExtrInfo(list);
        pageInfo.setList(storageIds);
        return WrapMapper.ok(pageInfo);
    }

}
